package com.anecdote.ideaplugins.changesbar;

import com.intellij.ide.BrowserUtil;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Copyright 2006 Nathan Brown
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
public class ChangesBarConfigurationForm
{
  private JCheckBox _addCommentEditorToCheckBox;
  private JPanel _settingsPanel;
  private JCheckBox _monitorChangelistsForConflictCheckBox;
  private JCheckBox _displayPopupWhenConflictCheckBox;
  private JLabel _titleLabel;
  private JPanel _root;
  private JEditorPane _creditsEditorPane;
  private JCheckBox _closeFilesCheckBox;
  private JCheckBox _openFilesFromActivatedCheckBox;
  private JCheckBox _openTabsInBackgroundCheckBox;
  private JCheckBox _fullyExpandActiveChangelistCheckBox;
  private JCheckBox _enableActiveChangelistScopeCheckBox;

  {
    final ActionListener listener = new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        updateForm();
      }
    };
    _monitorChangelistsForConflictCheckBox.addActionListener(listener);
    _enableActiveChangelistScopeCheckBox.addActionListener(listener);
    _openFilesFromActivatedCheckBox.addActionListener(listener);
    _titleLabel.setText(_titleLabel.getText() + " v" + ChangesBar.VERSION);
    _creditsEditorPane.setText("<html>\n" +
                               "  <body>\n" +
                               "    <p align=\"center\">\n" +
                               "      <b><font size=\"10pt\" face=\"arial\">&#169; Anecdote Software</font></b><font face=\"arial\"> \n" +
                               "      <a href=\"mailto:anecdotesoftware@yahoo.co.uk\">anecdotesoftware@yahoo.co.uk</a> \n" +
                               "      </font>\n" +
                               "    </p>\n" +
                               "    <p align=\"center\">\n" +
                               "      <font face=\"arial\">With many thanks to <b>Taras Tielkes</b> for Active \n" +
                               "      Changelist Scope support. </font>\n" +
                               "    </p>\n" +
                               "    <p align=\"center\">\n" +
                               "      <font face=\"arial\">Contribute online at the <a href=\"http://code.google.com/p/ideachangesbarplugin/\">Changes \n" +
                               "      Bar Plugin Google Code Project</a></font>\n" +
                               "    </p>\n" +
                               "  </body>\n" +
                               "</html>");
//    _creditsEditorPane.setBackground(null);
    _creditsEditorPane.setMinimumSize(_creditsEditorPane.getPreferredSize());
    _creditsEditorPane.addHyperlinkListener(new HyperlinkListener()
    {
      public void hyperlinkUpdate(HyperlinkEvent e)
      {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          BrowserUtil.launchBrowser(e.getURL().toString());
        }
      }
    });
  }

  ChangesBarConfigurationForm()
  {
  }

  private void updateForm()
  {
    _displayPopupWhenConflictCheckBox.setEnabled(_monitorChangelistsForConflictCheckBox.isSelected());
    _fullyExpandActiveChangelistCheckBox.setEnabled(_enableActiveChangelistScopeCheckBox.isSelected());
    _openTabsInBackgroundCheckBox.setEnabled(_openFilesFromActivatedCheckBox.isSelected());
  }

  void setData(ChangesBarApplicationComponent data)
  {
    _addCommentEditorToCheckBox.setSelected(ChangesBarApplicationComponent.isAddCommentEditor());
    _monitorChangelistsForConflictCheckBox.setSelected(ChangesBarApplicationComponent.isWatchForCollisions());
    _displayPopupWhenConflictCheckBox.setSelected(ChangesBarApplicationComponent.isShowPopup());
    _enableActiveChangelistScopeCheckBox.setSelected(ChangesBarApplicationComponent.isAddChangelistScope());
    _fullyExpandActiveChangelistCheckBox.setSelected(ChangesBarApplicationComponent.isExpandChangelistScope());
    _closeFilesCheckBox.setSelected(ChangesBarApplicationComponent.isCloseFilesWhenSwitchingChangelist());
    _openFilesFromActivatedCheckBox.setSelected(ChangesBarApplicationComponent.isOpenFilesWhenSwitchingChangelist());
    _openTabsInBackgroundCheckBox.setSelected(ChangesBarApplicationComponent.isOpenFilesInBackground());
    updateForm();
  }

  void getData(ChangesBarApplicationComponent data)
  {
    ChangesBarApplicationComponent.setAddCommentEditor(_addCommentEditorToCheckBox.isSelected());
    ChangesBarApplicationComponent.setWatchForCollisions(_monitorChangelistsForConflictCheckBox.isSelected());
    ChangesBarApplicationComponent.setShowPopup(_displayPopupWhenConflictCheckBox.isSelected());
    ChangesBarApplicationComponent.setAddChangelistScope(_enableActiveChangelistScopeCheckBox.isSelected());
    ChangesBarApplicationComponent.setExpandChangelistScope(_fullyExpandActiveChangelistCheckBox.isSelected());
    ChangesBarApplicationComponent.setCloseFilesWhenSwitchingChangelist(_closeFilesCheckBox.isSelected());
    ChangesBarApplicationComponent.setOpenFilesInBackground(_openTabsInBackgroundCheckBox.isSelected());
    ChangesBarApplicationComponent.setOpenFilesWhenSwitchingChangelist(_openFilesFromActivatedCheckBox.isSelected());
  }

  boolean isModified(ChangesBarApplicationComponent data)
  {
    if (_addCommentEditorToCheckBox.isSelected() != ChangesBarApplicationComponent.isAddCommentEditor()) {
      return true;
    }
    if (_monitorChangelistsForConflictCheckBox.isSelected() != ChangesBarApplicationComponent.isWatchForCollisions()) {
      return true;
    }
    if (_enableActiveChangelistScopeCheckBox.isSelected() != ChangesBarApplicationComponent.isAddChangelistScope()) {
      return true;
    }
    if (_fullyExpandActiveChangelistCheckBox.isSelected() != ChangesBarApplicationComponent.isExpandChangelistScope()) {
      return true;
    }
    if (_openFilesFromActivatedCheckBox.isSelected() !=
        ChangesBarApplicationComponent.isOpenFilesWhenSwitchingChangelist()) {
      return true;
    }
    if (_closeFilesCheckBox.isSelected() != ChangesBarApplicationComponent.isCloseFilesWhenSwitchingChangelist()) {
      return true;
    }
    if (_openTabsInBackgroundCheckBox.isSelected() != ChangesBarApplicationComponent.isOpenFilesInBackground()) {
      return true;
    }
    return _displayPopupWhenConflictCheckBox.isSelected() != ChangesBarApplicationComponent.isShowPopup();
  }

  JComponent getRootComponent()
  {
    return _root;
  }

  public static void main(String[] args)
  {
    JFrame frame = new JFrame("Test Frame");
    ChangesBarConfigurationForm component = new ChangesBarConfigurationForm();
    frame.getContentPane().add(component.getRootComponent(), BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }
}
